"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin
Case Name   : 验证mysql侧mod操作符
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建b库
    2、其中一个b库下创建插件
    3、验证mod操作符
    4、mod无效值测试
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、报错
    5、清理环境完成
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PluginTest012(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0012'

    def test_dolphin(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')

        text = '----step1:创建b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '---step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step3:验证mod操作符;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''select 8mod3;
            select 8 mod3;
            select 8 mod 3;
            select 8 mod 0;
            select 8mod 3, 8mod 3mod2, 8mod 3mod 2;
            select 0 mod 8;'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        strip_cmd = [x.strip() for x in sql_res.splitlines() if
                     x.strip() != '']
        self.log.info(strip_cmd)
        self.assertTrue(strip_cmd.count('8') == 2, '执行失败:' + text)
        self.assertTrue(sql_res.splitlines()[-2].strip() == '0',
                        '执行失败:' + text)
        self.assertEqual('2 |    2 |     0', sql_res.splitlines()[-7].strip(),
                         '执行失败:' + text)

        text = '-----step4:mod无效值测试;expect:报错---'
        self.log.info(text)
        sql_cmd = f'''select '-12.3abc' mod null;
            select '-12.3abc' mod -100.1;
            select '-12.3abc' mod 0;
            select '-12.3abc' mod 5;
            select '-12.3abc' mod 158.3;
            select '-12.3abc' mod -8.222e4;
            select '-12.3abc' mod true;
            select '-12.3abc' mod false;
            select '-12.3abc' mod 'null';'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('invalid input syntax for type numeric: "-12.3abc"',
                      sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '--step5:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.log.info('断言teardown成功')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
